package com.nsi.o2o.interceptor.shopadmin;

import com.nsi.o2o.entity.PersonInfo;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * @author: Luo Zhen
 * @date: 2018/11/7 11:48
 * @description: 登陆拦截器
 */
public class ShopLoginInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object userObj = request.getSession().getAttribute("user");
        if (userObj != null) {
            // 若用户信息不为空则将session里用户账号转换成PersonInfo实体类
            PersonInfo user = (PersonInfo) userObj;
            // 验证账号是否是可用状态
            if (user != null && user.getUserId() != null && user.getUserId() > 0 && user.getEnableStatus() == 1) {
                return true;
            }
        }
        //若不满足条件，直接跳转到账号登陆页面
        PrintWriter out = response.getWriter();
        out.write("<html>");
        out.write("<script>");
        out.write("window.open('" + request.getContextPath() + "/local/login?usertype=2','_self')");
        out.write("</script>");
        out.write("</html>");
        return false;
    }
}
